package com.itheima.crops.controller;

import com.github.pagehelper.Page;
import com.itheima.crops.dao.DiseaseMapper;
import com.itheima.crops.pojo.Index;
import com.itheima.crops.service.IndexService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexService indexService;

    /**
     * 农作物指标全查
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Index> indexList = indexService.findAll();
            return new Result(true, StatusCode.OK,"查询指标成功！",indexList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询指标失败！");
        }
    }

    /**
     * 农作物指标根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Index index = indexService.findById(id);
            return new Result(true,StatusCode.OK,"根据id查询指标成功！",index);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物指标不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"根据id查询指标失败，该农作物病虫害不存在！");
            }
            return new Result(false,StatusCode.ERROR,"根据id查询指标失败！");
        }
    }

    /**
     * 新增农作物
     * @param index
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Index index) {
        try {
            indexService.add(index);
            return new Result(true,StatusCode.OK,"新增农作物指标成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"新增农作物指标失败！");
        }
    }


    /**
     * 修改农作物指标
     * @param index
     */
    @PostMapping("/update")
    public Result update(@RequestBody Index index) {
        try {
            indexService.update(index);
            return new Result(true,StatusCode.OK,"修改农作物指标成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物指标不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"修改农作物指标失败，该农作物指标不存在！");
            }
            return new Result(false,StatusCode.ERROR,"修改农作物指标失败！");
        }
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable(name = "page")  int page, @PathVariable(name = "size")  int size){
        try {
            Page<Index> pageList = indexService.findPage(searchMap, page, size);
            PageResult pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询成功");
        }
    }

    /**
     * 删除农作物指标
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        try {
            indexService.delete(id);
            return new Result(true,StatusCode.OK,"删除农作物指标成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物指标不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"删除农作物指标失败，该农作物指标不存在！");
            }
            if("该农作物指标与农作物关联".equals(e.getMessage())) {
                return new Result(false,StatusCode.EXISTREL,"删除农作物指标失败，该农作物指标与农作物或者设备关联！");
            }
            if("该农作物指标与设备关联".equals(e.getMessage())) {
                return new Result(false,StatusCode.EXISTREL,"删除农作物指标失败，该农作物指标与设备关联！");
            }
            return new Result(false,StatusCode.ERROR,"删除农作物指标失败！");
        }
    }
}
